package com.estore.action;

import java.util.Map;

import com.estore.converter.DateTime;
import com.estore.converter.MD5;
import com.estore.info.AccountInfo;
import com.estore.info.ProfileInfo;
import com.estore.info.RegisterInfo;
import com.estore.info.ShopInfo;
import com.estore.manager.ProfileManager;
import com.estore.manager.ShopManager;
import com.estore.model.RegisterModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegisterAction extends ActionSupport implements
		ModelDriven<RegisterModel> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4881829561292330303L;
	RegisterModel registerModel = new RegisterModel();
	ProfileManager profileManager;
	ShopManager shopManager;
	DateTime dateTime = new DateTime();

	public String shopRegister() {
		registerModel.setErrorEmail("");
		registerModel.setErrorAccount("");
		registerModel.setErrorPhoneNumber("");
		registerModel.setRegisterSuccess("");
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else {

				boolean check = true;
				if (registerModel.getShopInfo() != null) {
					ShopInfo info = registerModel.getShopInfo();
					String userID = session.get("userID").toString();
					info.setUserID(Integer.parseInt(userID));
					String currentDate = dateTime.currentDate();
					info.setStartDate(currentDate);
					if (info.getAgentName().equals("")
							|| info.getAgentWebsite().equals("")
							|| info.getAgentEmail().equals("")
							|| info.getPhoneNumber().equals("")
							|| info.getAddress().equals("")) {
						registerModel
						.setRegisterSuccess("Vui lòng điền đầy đủ thông tin");
						check = false;
					}
					if (shopManager.checkEmailAvail(info.getAgentEmail())) {
						registerModel.setErrorEmail("Email đã được sử dụng");
						check = false;
					}
					if (shopManager.checkAgentNameAvail(info.getAgentName())) {
						registerModel
						.setErrorAccount("Tên cửa hàng đã được đăng kí");
						check = false;
					}
					if (shopManager
							.checkPhoneNumberAvail(info.getPhoneNumber())) {
						registerModel
						.setErrorPhoneNumber("Số điện thoại đã được sử dụng");
					}
					if (check == true) {
						shopManager.regShop(info);
						registerModel.setRegisterSuccess("Đăng ký thành công");
						info.setLogo("img/shopLogo/logo.png");
						ShopInfo shopInfo = shopManager.getShopInfo(Integer.parseInt(userID));
						shopManager.setDefaultInterface(shopInfo.getAgentID());
						session.put("role", 5);
						session.put("shopInfo", shopInfo);
						session.put("shop", "Avail");
					}
				}
				if(check==false){
					return "checkfalse";
				}
				registerModel.setShopInfo(null);
				return SUCCESS;
			}
		} else {
			return "banned";
		}
	}

	public ShopManager getShopManager() {
		return shopManager;
	}

	public void setShopManager(ShopManager shopManager) {
		this.shopManager = shopManager;
	}

	public DateTime getDateTime() {
		return dateTime;
	}

	public void setDateTime(DateTime dateTime) {
		this.dateTime = dateTime;
	}

	public String register() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null || session.get("role").equals(1)) {
				boolean check = true;
				registerModel.setErrorAccount(null);
				registerModel.setErrorEmail(null);
				registerModel.setRegisterSuccess(null);
				RegisterInfo registerInfo = registerModel.getRegisterInfo();
				Integer year = Integer.parseInt(registerModel.getYear()) - 1900;
				String currentDate = dateTime.currentDate();
				String getDate = dateTime.getDate(year,
						Integer.parseInt(registerModel.getMonth()),
						Integer.parseInt(registerModel.getDay()));
				if (registerInfo.getAccount().equals("")
						|| registerInfo.getPassword().equals("")
						|| registerInfo.getFirstName().equals("")
						|| registerInfo.getLastName().equals("")
						|| registerInfo.getEmail().equals("")
						|| registerInfo.getSecretAnswer().equals("")
						|| registerModel.getDay().equals("-1")
						|| registerModel.getMonth().equals("-1")
						|| registerModel.getYear().equals("-1")
						|| registerInfo.getSecretQuestion().equals("-1")) {
					check = false;
				}
				if (registerModel.getRole() == null) {
					registerInfo.setRole(4);
				} else {
					registerInfo.setRole(registerModel.getRole());
				}
				registerInfo.setDateOfBirth(getDate);
				registerInfo.setRegDate(currentDate);
				String gentle = null;
				if (registerModel.getSex().equals("-1")) {
					gentle = "male";
				} else if (registerModel.getSex().equals("1")) {
					gentle = "female";
				}
				registerInfo.setSex(gentle);
				if (check == true) {
					if (profileManager.checkAccountAvail(registerInfo
							.getAccount())
							&& profileManager.checkEmailAvail(registerInfo
									.getEmail())) {
						registerModel
								.setErrorAccount("Tài Khoản đã được sử dụng !!!");
						registerModel
								.setErrorEmail("Email đã được sử dụng !!!");
						if (registerModel.getRole() == null) {
							return SUCCESS;
						} else {
							return "modSuccess";
						}

					} else if (!profileManager.checkAccountAvail(registerInfo
							.getAccount())
							&& profileManager.checkEmailAvail(registerInfo
									.getEmail())) {
						registerModel
								.setErrorEmail("Email đã được sử dụng !!! ");
						if (registerModel.getRole() == null) {
							return "success";
						} else {
							return "modSuccess";
						}
					} else if (profileManager.checkAccountAvail(registerInfo
							.getAccount())
							&& !profileManager.checkEmailAvail(registerInfo
									.getEmail())) {
						registerModel
								.setErrorAccount("Tài Khoản đã được sử dụng !!!");
						if (registerModel.getRole() == null) {
							return "success";
						} else {
							return "modSuccess";
						}
					} else {
						registerModel
								.setRegisterSuccess("Chúc Mừng bạn đã đăng kí thành công !!!");
						String pass = registerInfo.getPassword();
						registerInfo.setPassword(MD5.encryptMD5(pass));
						profileManager.register(registerInfo);
						if(session.get("role") == null || !session.get("role").equals(1)){
							AccountInfo infoAcc = profileManager.getAccountInfo(registerInfo.getAccount(),
									registerInfo.getPassword());
							ProfileInfo info = profileManager.getUserInfo(infoAcc.getUserID());
							session.put("logged-in", "true");
							session.put("userID", infoAcc.getUserID());
							session.put("role", info.getRole());
							session.put("isBanned", info.getIsBanned());
							session.put("username", info.getAccount());
							session.put("password", info.getPassword());
							session.put("firstName", info.getFirstName());
							session.put("lastName", info.getLastName());
							session.put("birthDay", info.getDateOfBirth());
							session.put("phoneNumber", info.getPhoneNumber());
							session.put("email", info.getEmail());
							session.put("address", info.getAddress());
							session.put("gentle", info.getSex());
							session.put("secretQuestion", info.getSecretQuestion());
							session.put("secretAnswer", info.getSecretAnswer());
							session.put("avatar", info.getAvatar());
							if (shopManager.checkAgentAvail(info.getUserID())) {
								session.put("shop", "Avail");
								if (shopManager.getShopInfo(infoAcc.getUserID()) != null) {
									ShopInfo shopInfo = shopManager.getShopInfo(infoAcc
											.getUserID());
									session.put("shopInfo", shopInfo);
								}
							} else {
								session.put("shop", "NotAvail");
							}
						}
//						RegisterInfo info = new RegisterInfo(null, null, null,
//								null, null, null, null);
//						registerModel.setRegisterInfo(info);
						if (registerModel.getRole() == null) {
							return "regsuccess";
						} else {
							return "modSuccess";
						}
					}
				} else {
					if (registerModel.getRole() == null) {
						return "success";
					} else {
						return "modSuccess";
					}
				}
			} else {

				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	@Override
	public RegisterModel getModel() {
		return registerModel;
	}

	public RegisterModel getRegisterModel() {
		return registerModel;
	}

	public void setRegisterModel(RegisterModel registerModel) {
		this.registerModel = registerModel;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
